﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Palindromes
{
    class PalindromCheckingAlgorithm
    {
        private char[] input;
        private int indexFromStart;
        private int indexFromEnd;

        public PalindromCheckingAlgorithm(string input)
        {
            this.input = input.ToCharArray(0, input.Length);
            indexFromStart = -1;
            indexFromEnd = input.Length;
        }

        private Boolean checkIfCanMove()
        {
            return indexFromStart < indexFromEnd;
        }

        private Boolean findNextCharIndexFromStart()
        {
            indexFromStart++;
            char currentSymbol = input[indexFromStart];
            Console.WriteLine("Просмотр индекса {0:D}", indexFromStart);
            while (indexFromStart < input.Length && indexFromStart < indexFromEnd && isDelimiter(currentSymbol))
            {
                indexFromStart++;
                Console.WriteLine("Просмотр индекса {0:D}", indexFromStart);
                currentSymbol = input[indexFromStart];
            }
            return indexFromStart == indexFromEnd;
        }

        private void findNextCharIndexFromEnd()
        {
            indexFromEnd--;
            char currentSymbol = input[indexFromEnd];
            Console.WriteLine("Просмотр индекса {0:D}", indexFromEnd);
            while (indexFromEnd > 0 && isDelimiter(currentSymbol))
            {
                indexFromEnd--;
                Console.WriteLine("Просмотр индекса {0:D}", indexFromEnd);
                currentSymbol = input[indexFromEnd];
            }
        }

        public Boolean check()
        {
            if (input.Length == 0)
            {
                return true;
            }
            Boolean canBePalindrome = true;
            do
            {
                Boolean isEnd = findNextCharIndexFromStart();
                if (isEnd)
                {
                    canBePalindrome = true;
                    continue;
                }
                findNextCharIndexFromEnd();
                if (indexFromStart < indexFromEnd)
                {
                    char c1 = input[indexFromStart];
                    char c2 = input[indexFromEnd];
                    Console.Write("Просматриваем символы {0} и {1}... ", c1, c2);
                    canBePalindrome = compareChars(c1, c2);
                    if (canBePalindrome)
                    {
                        Console.WriteLine("совпадение");
                    }
                    else
                    {
                        Console.WriteLine("несовпадение");
                    }
                }
                else if (indexFromStart == indexFromEnd)
                {
                    canBePalindrome = true;
                }
                else
                {
                    canBePalindrome = false;
                }

            } while (canBePalindrome && !isEndOfCheck());
            return canBePalindrome;
        }

        private Boolean compareChars(char c1, char c2)
        {
            string s1 = c1.ToString();
            string s2 = c2.ToString();
            return s1.Equals(s2, StringComparison.InvariantCultureIgnoreCase);

        }
        private Boolean isEndOfCheck()
        {
            return indexFromEnd - indexFromStart <= 1;
        }

        private Boolean isDelimiter(char c)
        {
            Boolean isDelim = c == 11 || (c >= 20 && c <= 47) || (c >= 58 && c <= 63) || c == 27 || (c >= 8192 && c <= 8447);
            if(isDelim){
                Console.WriteLine("{0} пропущен", c);
            }
            return isDelim;
        }
    }
}
